﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace PhotoXX
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure(new Uri(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log4net.config"));
            log4net.ILog CommonLog = log4net.LogManager.GetLogger("CommonLog");
            log4net.ILog Ex = log4net.LogManager.GetLogger("Ex");
            log4net.ILog NotFound = log4net.LogManager.GetLogger("NotFound");

            string source = System.Configuration.ConfigurationManager.AppSettings["Source"];
            string destination = System.Configuration.ConfigurationManager.AppSettings["Destination"];
            string csv = System.Configuration.ConfigurationManager.AppSettings["CSV"];
            string mode = System.Configuration.ConfigurationManager.AppSettings["Mode"];
            bool cut = mode == "Cut";
            bool copy = mode == "Copy";
            bool check = mode == "Check";

            Console.WriteLine("press any key to confirm starting...");
            Console.WriteLine("source: " + source);
            Console.WriteLine("destination: " + destination);
            Console.WriteLine("csv: " + csv);
            Console.WriteLine("mode: " + mode);
            Console.ReadKey();

            string[] csvFiles = Directory.GetFiles(csv, "*.csv");
            do
            {
                foreach (var csvFile in csvFiles)
                {
                    CommonLog.InfoFormat("start processing {0}", csvFile);

                    #region process csv file

                    string[] lines = File.ReadAllLines(csvFile);
                    string line = string.Empty;
                    int len = lines.Length;
                    for (int i = 1; i < len; i++)
                    {
                        line = lines[i];
                        if (string.IsNullOrEmpty(line))
                        {
                            CommonLog.Warn("encountered empty string");
                            continue;
                        }
                        try
                        {
                            string[] fields = line.Split(',');
                            if (fields.Length < 3)
                            {
                                CommonLog.WarnFormat("field count is less than 3 {0}", line);
                                continue;
                            }
                            string srcFile = Path.Combine(source, fields[1]);
                            if (!File.Exists(srcFile))
                            {
                                NotFound.Info(fields[0]);
                                continue;
                            }

                            if (check)
                            {
                                CommonLog.InfoFormat("end of checking no.{0}/{1} {2}", i, len, srcFile);
                                continue;
                            }

                            string destFile = Path.Combine(destination, fields[2]);
                            if (!Directory.Exists(Path.GetDirectoryName(destFile)))
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(destFile));
                            }

                            CommonLog.InfoFormat("start copying no.{0}/{1} {2} to {3}", i, len, srcFile, destFile);
                            if (cut)
                            {
                                if (File.Exists(destFile))
                                {
                                    CommonLog.WarnFormat("skip {0} with {1}", destFile, srcFile);
                                    continue;
                                }
                                File.Move(srcFile, destFile);
                            }
                            else
                            {
                                if (File.Exists(destFile))
                                {
                                    CommonLog.WarnFormat("skip {0} with {1}", destFile, srcFile);
                                    continue;
                                }
                                File.Copy(srcFile, destFile);
                            }
                        }
                        catch (Exception ex)
                        {
                            Ex.Info(line);
                            CommonLog.Fatal(ex);
                        }
                    }

                    #endregion

                    File.Move(csvFile, csvFile + ".bak");
                }

                csvFiles = Directory.GetFiles(csv, "*.csv");
            } while (csvFiles != null && csvFiles.Length > 0);

            Console.WriteLine("finish, press any key to continue...");
            Console.ReadKey();
        }
    }
}
